import axios from 'axios';

export default{
    promise_process (promise){
        return promise.then( data => [null, data.data])
                      .catch( err => [err, null]);
    },
    //请求所有公告
    async getNotices(){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/announcements',
            mode        : 'cors',
            method          : 'GET',
            withCredentials: true,
        })
        return response;
    },
    //增加公告
    async addNotice(notice){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/announcements',
            mode        : 'cors',
            method          : 'POST',
            withCredentials: true,
            headers       :{
                "Content-Type": 'application/json'
            },
            data          : notice
        })
        return response;
    },
    //修改公告
    async editNotice(notice){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/announcements',
            mode        : 'cors',
            method          : 'PUT',
            withCredentials: true,
            headers       :{
                "Content-Type": 'application/json'
            },
            data          : notice
        })
        return response;
    },

    //修改公告
    async deleteNotice(id){
        const response = await axios({
            url             : `http://127.0.0.1:5000/announcements/id/${id}`,
            mode        : 'cors',
            method          : 'DELETE',
            withCredentials: true,
        })
        return response;
    }
}
